<?php 

$db=New Controller_hello();


switch ($_GET['type']) {
	case '1':
		echo $db->Test('1176773465668194304'); //鼎盛原生-信息流
		break;
	case '2':
		echo $db->Test('1280370818093813760'); //鼎盛原生-SEO
		break;
    case '3':
        echo $db->YouJu('1122785705056145408');//优居接口
        
    case '4':

        $CusName='张晓春';
        $CusPhone='13234705808';
        $ChannelID='1122785705056145408';
        $CusAddress='内蒙呼伦贝尔';
        $Remark='';

        echo $db->zi($CusName,$CusPhone,$ChannelID,$CusAddress,$Remark);

    case '5':
        

        echo $db->newTest('1176773465668194304'); //鼎盛原生-信息流
        break;



	default:
		# code...
		break;
}



class Controller_hello
{

	public function Test($qudao){



		$input=file_get_contents("php://input");
		file_put_contents('json/data.json', $input);
		$requestBody=json_decode($input,true);
		if(!is_array($requestBody)||!is_array($requestBody['data'])){

			header('HTTP/1.1 400 Badrequest');
			exit;

		}


		//$requestBody=json_decode(file_get_contents('json/data.json'),true);

		// echo '<pre>';

		// var_dump($requestBody);

		// echo '</pre>';


		$site_id=$requestBody['site_id'];
		$page_id=$requestBody['page_id'];
		$name=$requestBody['data'][0]['value'];
		$phone=$requestBody['data'][1]['value'];
		$data_submit=date('Y-m-d H:i:s');
		//$qudao='鼎盛原生2';
		$city=self::selectCity($phone);

		$qure="INSERT INTO badu_seoPhone (site_id,page_id,name,phone,date_submit,city,qudao) VALUE('{$site_id}','{$page_id}','{$name}','{$phone}','{$data_submit}','{$city}','{$qudao}');";
		$host='localhost';
		$user='root';
		$password='root';
		$database='weizandata';
		$link=mysqli_connect($host, $user, $password, $database);
		$link->query("set names utf8");

		$result=mysqli_query($link, $qure);


		if($result){

            self::zi($name,$phone,$qudao,$city,'');

		}else{

			$str='语句：'.$qure.' 错误代码: '.mysqli_errno($link).' 错误解释: '.mysqli_error($link).'时间：'. date("Y-m-d H:i:s");

            file_put_contents('json/erro.json', $str.PHP_EOL,FILE_APPEND);

		}

		mysqli_close($link);

        return 'success';

	}

    public function newTest($qudao){ //新版的百度api



        $input=file_get_contents("php://input");
        file_put_contents('json/data.json', $input);
        $requestBody=json_decode($input,true);

        // if(!is_array($requestBody)||!is_array($requestBody['data'])){

        //     header('HTTP/1.1 400 Badrequest');
        //     exit;

        // }


        // $requestBody=json_decode(file_get_contents('json/data.json'),true);

        // echo '<pre>';

        // var_dump($requestBody);

        // echo '</pre>';


        $site_id=$requestBody['site_id'];
        $page_id=$requestBody['page_id'];
        $name="客户";
        $phone=$requestBody['phone'];
        $data_submit=date('Y-m-d H:i:s');
        //$qudao='鼎盛原生2';
        $city=self::selectCity($phone);

        $qure="INSERT INTO badu_seoPhone (site_id,page_id,name,phone,date_submit,city,qudao) VALUE('{$site_id}','{$page_id}','{$name}','{$phone}','{$data_submit}','{$city}','{$qudao}');";
        $host='localhost';
        $user='root';
        $password='root';
        $database='weizandata';
        $link=mysqli_connect($host, $user, $password, $database);
        $link->query("set names utf8");

        $result=mysqli_query($link, $qure);


        if($result){

            self::zi($name,$phone,$qudao,$city,'');

        }else{

            $str='语句：'.$qure.' 错误代码: '.mysqli_errno($link).' 错误解释: '.mysqli_error($link).'时间：'. date("Y-m-d H:i:s");

            file_put_contents('json/erro.json', $str.PHP_EOL,FILE_APPEND);

        }

        mysqli_close($link);

        return 'success';

    }

    public function YouJu($qudao){



        $input=file_get_contents("php://input");
        file_put_contents('json/data.json', $input);
        $requestBody=json_decode($input,true);
        if(!is_array($requestBody)){

            header('HTTP/1.1 400 Badrequest');
            exit;

        }


        //$requestBody=json_decode(file_get_contents('json/data.json'),true);

        // echo '<pre>';

        // var_dump($requestBody);

        // echo '</pre>';


        $data_submit=date('Y-m-d H:i:s');

        $id=$requestBody['id'];
        $title=$requestBody['title'];
        $ad_position_id=$requestBody['ad_position_id'];
        $link_url=$requestBody['link_url'];
        $images=$requestBody['images'];
        $start_date=$requestBody['start_date'];
        $status=$requestBody['status'];
        $closed=$requestBody['closed'];
        $orderby=$requestBody['orderby'];
        $area=$requestBody['area'];
        $province=$requestBody['province'];
        $city=$requestBody['city'];
        $special=$requestBody['special'];
        $name=$requestBody['name'];

        //$qudao='鼎盛原生2';
        $city=self::selectCity($phone);

        $qure="INSERT INTO youjuTell (id,title,ad_position_id,link_url,images,start_date,status,closed,orderby,area,province,city,special,name,data_submit,isSub) VALUE('{$id}','{$title}','{$ad_position_id}','{$link_url}','{$images}','{$start_date}','{$status}','{$closed}','{$orderby}','{$area}','{$province}','{$city}','{$special}','{$name}','{$data_submit}',0);";

        $host='localhost';
        $user='root';
        $password='root';
        $database='weizandata';
        $link=mysqli_connect($host, $user, $password, $database);
        $link->query("set names utf8");

        $result=mysqli_query($link, $qure);


        if($result){

            self::zi($name,$phone,$qudao,$city,'');

        }else{

            $str='语句：'.$qure.' 错误代码: '.mysqli_errno($link).' 错误解释: '.mysqli_error($link).'时间：'. date("Y-m-d H:i:s");

            file_put_contents('json/erro.json', $str.PHP_EOL,FILE_APPEND);

        }

        mysqli_close($link);

        return 'success';

    }
	private function selectCity($phone){


			$phoneD=substr($phone,0,7);
			$qure="SELECT CONCAT(province,city) AS city FROM phone WHERE phone='{$phoneD}';";
			$host='localhost';
			$user='root';
			$password='root';
			$database='phone';
			$link=mysqli_connect($host, $user, $password, $database);
			$link->query("set names utf8");

			$result=mysqli_query($link, $qure);
			$arr=mysqli_fetch_all($result, 1);
			mysqli_close($link);
			return $arr[0]['city'];


	}

    private function do_curl($url, $params = array(), $upload = false, $type = 'POST')
    {
        
        $token = file_get_contents("tt/token.txt");

        if(!$token){
            self::gettoken();
            $token = file_get_contents("tt/token.txt");
        }

        $method = strtoupper($type);
        if ($method == 'GET') {
            $url = "{$url}?" . http_build_query($params);
        }
        $ch = curl_init();
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        //输出主机头
        if ($url=="https://capi.qumei.com/api/home/call"){
            //定义请求类型
            $username="webuser";
            $password="Qumei1993";
            $timestamp=time();

            $sign = md5($username.",".$password.",".$timestamp);

            $headers = array(
                "Content-Type: application/json",
                "Authorization: Bearer $token",
                "username: $username",
                "password: $password",
                "timestamp: $timestamp",
                "sign: $sign"
            );

            // foreach ($headers as $v){
            //    // echo $v."\r\n";
            // }

            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 是否检测服务器的域名与证书上的是否一致
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        if ($method == 'POST') {
            if ($upload) {        //设置上传文件
                $file = new \CURLFile($upload['tmp_name'], $upload['type'], $upload['name']);
                $params[$upload['get_name']] = $file;
            }
            //$params = '{"Service":"com.QM.CallCenter.CommonChannelAPI","Param":{\"CusName\":\"test591\u5ba2\u8d44\uff08\u8bf7\u5ffd\u7565\uff09\",\"CusPhone\":\"15107984564\",\"ChannelID\":\"1262592253055078400\",\"CusAddress\":\"\u5317\u4eac\",\"Remark\":\"\"}}';
            @curl_setopt($ch, CURLOPT_POST, 1);
            @curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }
        $result = curl_exec($ch);
        //$request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
        //print_r($request_header);
        //打印发送头信息
        curl_close($ch);
        if ($result === null || $result === false) {
            $this->error('操作出错！');
        } else {
            return $result;
        }
    }

    private function gettoken()
    {
        $data = array();
        $data['grant_type']="password";
        $data['username']="webuser";
        $data['password']="Qumei1993";
        $out = self::do_curl("https://capi.qumei.com/token",$data,"","POST");
        $out = json_decode($out,true);
        file_put_contents("tt/token.txt",$out["access_token"]);
    //var_dump($out["access_token"]);
    }  


    public function zi($CusName,$CusPhone,$ChannelID,$CusAddress,$Remark='')
    {

            $data["CusName"]= $CusName;
            $data["CusPhone"]= $CusPhone;
            $data["ChannelID"]= $ChannelID;
            $data["CusAddress"]= $CusAddress;
            $data["Remark"]= $Remark;
            $data = json_encode($data);
            $pp["Service"] = "com.QM.CallCenter.CommonChannelAPI";
            $pp["Param"] = addslashes($data);

            $pp = json_encode($pp);
            $pp = str_replace('\\\\\\',"\\",$pp);

            $out = self::do_curl("https://capi.qumei.com/api/home/call",$pp,false,"POST");
            $out = json_decode($out,true);

            if ($out['StatusCode']==200){

                 return '提交成功';

            }else{

                 return '提交失败';

            }

    }


}



 ?>